﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BasicDataManagement;
using DataModel;

namespace StockManagement
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        IOpertation op;
        List<StockRecordItem> source;

        SystemUser user;

        private MainWindow()
        {
            InitializeComponent();

            op = OperationFactory.Create();
        }

        public MainWindow(SystemUser user)
            : this()
        {
            this.user = user;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var matCatList = op.LoadMaterialCategory();
            foreach (var t in matCatList)
            {
                TreeViewItem tmpTVI = new TreeViewItem();
                tmpTVI.Header = t;
                tmpTVI.IsExpanded = true;
                tmpTVI.Selected += new RoutedEventHandler(treeVICat_Selected);
                treeVIMaterial.Items.Add(tmpTVI);
                var matList = op.LoadMateriaclItemName(t);
                foreach (var tI in matList)
                {
                    TreeViewItem tmpItemTVI = new TreeViewItem();
                    tmpItemTVI.Header = tI;
                    tmpItemTVI.ContextMenu = this.FindResource("AddMaterialCM") as ContextMenu;
                    tmpItemTVI.ContextMenuOpening += new ContextMenuEventHandler(tmpItemTVI_ContextMenuOpening);
                    tmpItemTVI.Selected += new RoutedEventHandler(treeVIMat_Selected);
                    tmpTVI.Items.Add(tmpItemTVI);
                }
            }
        }

        void tmpItemTVI_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            (sender as TreeViewItem).IsSelected = true;
        }

        private void treeVIMat_Selected(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show((sender as TreeViewItem).Header.ToString());
            source = op.LoadMaterialStockRecordsMaterial((treeViewStock.SelectedItem as TreeViewItem).Header.ToString());
            displaySource();
            //listViewStockRec.ItemsSource = new ObservableCollection<StockRecordItem>(op.LoadMaterialStockRecordsMaterial((treeViewStock.SelectedItem as TreeViewItem).Header.ToString()));
        }

        private void treeVICat_Selected(object sender, RoutedEventArgs e)
        {
            if (sender.Equals(e.Source))
            {
                //MessageBox.Show((sender as TreeViewItem).Header.ToString());
                source = op.LoadMaterialStockRecordsCategory((treeViewStock.SelectedItem as TreeViewItem).Header.ToString());
                displaySource();
                //listViewStockRec.ItemsSource = new ObservableCollection<StockRecordItem>(op.LoadMaterialStockRecordsCategory((treeViewStock.SelectedItem as TreeViewItem).Header.ToString()));
            }
        }
 
        private void treeVIMaterialAll_Selected(object sender, RoutedEventArgs e)
        {
            if (sender.Equals(e.Source))
            {
                source = op.LoadMaterialStockRecordsAll();
                displaySource();
                //listViewStockRec.ItemsSource = new ObservableCollection<StockRecordItem>(op.LoadMaterialStockRecordsAll());
            }
        }

        private void AddMaterialMI_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(treeViewStock.SelectedItem.ToString());
            AddMaterialStockWindow win = new AddMaterialStockWindow();
            win.nameTB.Text = (treeViewStock.SelectedItem as TreeViewItem).Header.ToString();
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            var matObjs = op.LoadMaterialObjects();
            var matname = (treeViewStock.SelectedItem as TreeViewItem).Header.ToString();
            win.unitLabel.Content = (from c in matObjs
                                     where c.ObjectName == matname
                                     select c).First().Unit;
            if (win.ShowDialog() == true)
            {
                op.AddMaterialStockRecord(win.stockRecItem);
                source = op.LoadMaterialStockRecordsMaterial((treeViewStock.SelectedItem as TreeViewItem).Header.ToString());
                displaySource();
                //listViewStockRec.ItemsSource = new ObservableCollection<StockRecordItem>(op.LoadMaterialStockRecordsMaterial((treeViewStock.SelectedItem as TreeViewItem).Header.ToString()));
            }
        }

        private void displaySource()
        {
            if (source == null)
            {
                return;
            }
            listViewStockRec.ItemsSource = null;
            if (putinCB.IsChecked == true && takeoutCB.IsChecked == true)
            {
                listViewStockRec.ItemsSource =  new ObservableCollection<StockRecordItem>(source);
                return;
            }
            if (putinCB.IsChecked == true && takeoutCB.IsChecked != true)
            {
                var sourceSelected = (from c in source
                          where c.StockDir == StockDirection.PutIn
                          select c).ToList();
                listViewStockRec.ItemsSource = new ObservableCollection<StockRecordItem>(sourceSelected);
                return;
            }
            if (putinCB.IsChecked != true && takeoutCB.IsChecked == true)
            {
                var sourceSelected = (from c in source
                          where c.StockDir == StockDirection.TakeOut
                          select c).ToList();
                listViewStockRec.ItemsSource = new ObservableCollection<StockRecordItem>(sourceSelected);
                return;
            }
        }

        private void directionCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            displaySource();
        }

        private void btnViewStock_Click(object sender, RoutedEventArgs e)
        {
            PurchaseWindow win = new PurchaseWindow();
            win.ShowDialog();
        }
   }
}
